package com.maple.controller;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author <a href=mailto:leihuazhe@gmail.com>maple</a>
 * @since 2018-10-21 8:57 PM
 */
@RestController
public class LogController {
    private static Logger logger = LoggerFactory.getLogger(LogController.class);

    @RequestMapping(value = "level/{level}")
    public String changeLogLevel(@PathVariable("level") String logLevel) {

        try {
            LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
            loggerContext.getLogger("ROOT").setLevel(Level.valueOf(logLevel));
//            loggerContext.getLogger("org.mybatis").setLevel(Level.valueOf(logLevel));
//            loggerContext.getLogger("org.springframework").setLevel(Level.valueOf(logLevel));
        } catch (Exception e) {
            logger.error("动态修改日志级别出错", e);
            return "fail";
        }
        logger.debug("set logger level successful ");
        return "success";
    }


}
